/************************************************************************************/
/*  Program name CAPFAIMP_MASS_ADDITIONS.sql                                        */
/*  Creater:  NIP Karen (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 17 JAN, 2006   NIP Karen         First created                               	*/
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

ALTER TRIGGER CAP_FA_MASS_ADDITIONS DISABLE;
ALTER TRIGGER CSE_MASS_ADDITIONS_ARU DISABLE;
--ALTER TRIGGER FA_CUA_MASS_ADDITIONS DISABLE;
ALTER TRIGGER FA_MASS_ADDITIONS_BRI DISABLE;
ALTER TRIGGER FA_MASS_ADDITIONS_BRU DISABLE;
ALTER TRIGGER FA_MASS_ADDITIONS_DELETE_DIST DISABLE;
ALTER TRIGGER FA_MASS_ADDITIONS_INSERT_DIST DISABLE;
ALTER TRIGGER FA_MASS_ADDITIONS_INSERT_NAME DISABLE;
ALTER TRIGGER FA_MASS_ADDITIONS_UPDATE_DIST DISABLE;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFAIMP_MASS_ADDITIONS';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_FA_MASS_ADDITIONS;
		
	lv_Next_Seq_ID		NUMBER;
	lv_COA_ID			NUMBER;

BEGIN
	UPDATE
		FA_MASS_ADDITIONS
	SET	
		BOOK_TYPE_CODE	=	CAPBRIMP_MAPPING.RenameStr(SUBSTR(BOOK_TYPE_CODE,1,4), BOOK_TYPE_CODE),
		ASSET_NUMBER	=	CAPBRIMP_MAPPING.RenameStr(SUBSTR(ASSET_NUMBER,1,4), ASSET_NUMBER)
	WHERE BOOK_TYPE_CODE IN
		(
		SELECT 
			BOOK_TYPE_CODE
		FROM 
			CRST_FA_MASS_ADDITIONS
		);
	
	FOR C1 IN Cur_F LOOP
		BEGIN
			SELECT	FA_MASS_ADDITIONS_S.NextVal
			INTO	lv_Next_Seq_ID
			FROM	DUAL;
			
			INSERT INTO CRST_FA_MASS_ADDITIONS_MAP 
			VALUES
			(
			C1.MASS_ADDITION_ID,   
			lv_Next_Seq_ID
			);
			
			lv_COA_ID := CAPBRIMP_COMMON.GetCOAIDfromBTC(C1.Book_Type_Code);
		
			INSERT INTO FA_MASS_ADDITIONS
			VALUES
			(
			lv_Next_Seq_ID,
			C1.ASSET_NUMBER,
			C1.TAG_NUMBER,
			C1.DESCRIPTION,
			C1.ASSET_CATEGORY_ID,
			C1.MANUFACTURER_NAME,
			C1.SERIAL_NUMBER,
			C1.MODEL_NUMBER,
			C1.BOOK_TYPE_CODE,
			C1.DATE_PLACED_IN_SERVICE,
			C1.FIXED_ASSETS_COST,
			C1.PAYABLES_UNITS,
			C1.FIXED_ASSETS_UNITS,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.PAYABLES_CODE_COMBINATION_ID_D),
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.EXPENSE_CODE_COMBINATION_ID),
			C1.LOCATION_ID,
			C1.ASSIGNED_TO,
			C1.FEEDER_SYSTEM_NAME,
			C1.CREATE_BATCH_DATE,
			C1.CREATE_BATCH_ID,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_NAME),
			C1.REVIEWER_COMMENTS,
			C1.INVOICE_NUMBER,
			C1.VENDOR_NUMBER,
				C1.PO_VENDOR_ID,
			C1.PO_NUMBER,
			C1.POSTING_STATUS,
			C1.QUEUE_NAME,
			C1.INVOICE_DATE,
			C1.INVOICE_CREATED_BY,
			C1.INVOICE_UPDATED_BY,
			C1.PAYABLES_COST,
			CAPBRIMP_MAPPING.GetAPInvoiceIDMapping(C1.INVOICE_ID),
			C1.PAYABLES_BATCH_NAME,
			C1.DEPRECIATE_FLAG,
			C1.PARENT_MASS_ADDITION_ID,
				C1.PARENT_ASSET_ID,
			C1.SPLIT_MERGED_CODE,
			C1.AP_DISTRIBUTION_LINE_NUMBER,
			C1.POST_BATCH_ID,
				C1.ADD_TO_ASSET_ID,
			C1.AMORTIZE_FLAG,
			C1.NEW_MASTER_FLAG,
			CAPBRIMP_MAPPING.GetAssetKeyMapping(C1.ASSET_KEY_CCID),
			C1.ASSET_TYPE,
			C1.DEPRN_RESERVE,
			C1.YTD_DEPRN,
			C1.BEGINNING_NBV,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_NAME),
			C1.CREATION_DATE,
			C1.LAST_UPDATE_LOGIN,
			C1.SALVAGE_VALUE,
			C1.ACCOUNTING_DATE,
			C1.ATTRIBUTE1,
			C1.ATTRIBUTE2,
			C1.ATTRIBUTE3,
			C1.ATTRIBUTE4,
			C1.ATTRIBUTE5,
			C1.ATTRIBUTE6,
			C1.ATTRIBUTE7,
			C1.ATTRIBUTE8,
			C1.ATTRIBUTE9,
			C1.ATTRIBUTE10,
			C1.ATTRIBUTE11,
			C1.ATTRIBUTE12,
			C1.ATTRIBUTE13,
			C1.ATTRIBUTE14,
			C1.ATTRIBUTE15,
			C1.ATTRIBUTE_CATEGORY_CODE,
			C1.FULLY_RSVD_REVALS_COUNTER,
			C1.MERGE_INVOICE_NUMBER,
			C1.MERGE_VENDOR_NUMBER,
			C1.PRODUCTION_CAPACITY,
			C1.REVAL_AMORTIZATION_BASIS,
			C1.REVAL_RESERVE,
			C1.UNIT_OF_MEASURE,
			C1.UNREVALUED_COST,
			C1.YTD_REVAL_DEPRN_EXPENSE,
			C1.ATTRIBUTE16,
			C1.ATTRIBUTE17,
			C1.ATTRIBUTE18,
			C1.ATTRIBUTE19,
			C1.ATTRIBUTE20,
			C1.ATTRIBUTE21,
			C1.ATTRIBUTE22,
			C1.ATTRIBUTE23,
			C1.ATTRIBUTE24,
			C1.ATTRIBUTE25,
			C1.ATTRIBUTE26,
			C1.ATTRIBUTE27,
			C1.ATTRIBUTE28,
			C1.ATTRIBUTE29,
			C1.ATTRIBUTE30,
			C1.MERGED_CODE,
			C1.SPLIT_CODE,
				C1.MERGE_PARENT_MASS_ADDITIONS_ID,
				C1.SPLIT_PARENT_MASS_ADDITIONS_ID,
			C1.PROJECT_ASSET_LINE_ID,
			C1.PROJECT_ID,
			C1.TASK_ID,
			C1.SUM_UNITS,
			C1.DIST_NAME,
			C1.GLOBAL_ATTRIBUTE1,
			C1.GLOBAL_ATTRIBUTE2,
			C1.GLOBAL_ATTRIBUTE3,
			C1.GLOBAL_ATTRIBUTE4,
			C1.GLOBAL_ATTRIBUTE5,
			C1.GLOBAL_ATTRIBUTE6,
			C1.GLOBAL_ATTRIBUTE7,
			C1.GLOBAL_ATTRIBUTE8,
			C1.GLOBAL_ATTRIBUTE9,
			C1.GLOBAL_ATTRIBUTE10,
			C1.GLOBAL_ATTRIBUTE11,
			C1.GLOBAL_ATTRIBUTE12,
			C1.GLOBAL_ATTRIBUTE13,
			C1.GLOBAL_ATTRIBUTE14,
			C1.GLOBAL_ATTRIBUTE15,
			C1.GLOBAL_ATTRIBUTE16,
			C1.GLOBAL_ATTRIBUTE17,
			C1.GLOBAL_ATTRIBUTE18,
			C1.GLOBAL_ATTRIBUTE19,
			C1.GLOBAL_ATTRIBUTE20,
			C1.GLOBAL_ATTRIBUTE_CATEGORY,
			C1.CONTEXT,
			C1.INVENTORIAL,
			C1.SHORT_FISCAL_YEAR_FLAG,
			C1.CONVERSION_DATE,
			C1.ORIGINAL_DEPRN_START_DATE,
			C1.GROUP_ASSET_ID,
			C1.CUA_PARENT_HIERARCHY_ID,
			C1.UNITS_TO_ADJUST,
			C1.BONUS_YTD_DEPRN,
			C1.BONUS_DEPRN_RESERVE,
			C1.AMORTIZE_NBV_FLAG,
			C1.AMORTIZATION_START_DATE,
			C1.TRANSACTION_TYPE_CODE,
			C1.TRANSACTION_DATE,
			C1.WARRANTY_ID,
			C1.LEASE_ID,
			C1.LESSOR_ID,
			C1.PROPERTY_TYPE_CODE,
			C1.PROPERTY_1245_1250_CODE,
			C1.IN_USE_FLAG,
			C1.OWNED_LEASED,
			C1.NEW_USED,
				C1.ASSET_ID
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;
	
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/

ALTER TRIGGER CAP_FA_MASS_ADDITIONS ENABLE;
ALTER TRIGGER CSE_MASS_ADDITIONS_ARU ENABLE;
--ALTER TRIGGER FA_CUA_MASS_ADDITIONS ENABLE;
ALTER TRIGGER FA_MASS_ADDITIONS_BRI ENABLE;
ALTER TRIGGER FA_MASS_ADDITIONS_BRU ENABLE;
ALTER TRIGGER FA_MASS_ADDITIONS_DELETE_DIST ENABLE;
ALTER TRIGGER FA_MASS_ADDITIONS_INSERT_DIST ENABLE;
ALTER TRIGGER FA_MASS_ADDITIONS_INSERT_NAME ENABLE;
ALTER TRIGGER FA_MASS_ADDITIONS_UPDATE_DIST ENABLE;